﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCHSHue.BLL;

namespace TCHSHue.Modules.AdminControl.HoiDap
{
    public partial class ctrHoiDapList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int pCurrent = getPageCurrent();
                NapDanhSachTinhTrang();
                NapDanhSachHoiDap(ddlTinhTrang.SelectedValue, pCurrent);
            }
        }

        void NapDanhSachTinhTrang()
        {
            List<TinhTrang> ls = new List<TinhTrang>();
            ls.Add(new TinhTrang() { Ma = -1, TenHienThi = "Tất cả" });
            ls.Add(new TinhTrang() { Ma = (int)TinhTrangBaiViet.ChuaDuyet, TenHienThi = "Chưa duyệt" });
            ls.Add(new TinhTrang() { Ma = (int)TinhTrangBaiViet.DaDuyet, TenHienThi = "Đã duyệt" });
            ddlTinhTrang.DataSource = ls;
            ddlTinhTrang.DataTextField = "TenHienThi";
            ddlTinhTrang.DataValueField = "Ma";
            ddlTinhTrang.DataBind();
        }

        protected void RedirectEdit(object sender, CommandEventArgs e)
        {
            Response.Redirect("~/Modules/Admin.aspx?mode=chinhhoidap&id=" + e.CommandArgument.ToString());
        }

        protected void Delete(object sender, CommandEventArgs e)
        {
            if (!HoiDapBLL.delete(e.CommandArgument.ToString()))
                Page.RegisterClientScriptBlock("XoaHoiDap", "<SCRIPT Language='JavaScript'> alert('Không thể xóa hỏi đáp này'); </SCRIPT>");
            else
                NapDanhSachHoiDap(ddlTinhTrang.SelectedValue, getPageCurrent());
        }

        private void NapDanhSachHoiDap(string TinhTrang, int pCurrent)
        {
            var tinhTrang = int.Parse(TinhTrang);
            var ls = new List<GridHoiDap>();
            int tsBai = 0;
            if (tinhTrang != -1)
            {
                ls = HoiDapBLL.getListTheoTrang(Hang.PAGESIZE, pCurrent, tinhTrang);
                tsBai = HoiDapBLL.getToTal(tinhTrang);
            }
            else
            {
                ls = HoiDapBLL.getListTheoTrang(Hang.PAGESIZE, pCurrent);
                tsBai = HoiDapBLL.getToTal();
            }
            gridHoiDap.DataSource = ls;
            gridHoiDap.DataBind();
            PhanTrang(tsBai, pCurrent, "../Modules/Admin.aspx?mode=hoidap&");
        }

        protected void bntLoc_Click(object sender, EventArgs e)
        {
            NapDanhSachHoiDap(ddlTinhTrang.SelectedValue, getPageCurrent());
        }

        int getPageCurrent()
        {
            var pageIndex = Request.QueryString["pageIndex"];
            int temp;
            int pCurrent = 1;
            if (!int.TryParse(pageIndex, out temp))
                pCurrent = 1;
            else
                pCurrent = int.Parse(pageIndex);
            return pCurrent;
        }

        void PhanTrang(int total, int pCurrent, string baseLink)
        {
            Paging paGing = new Paging(Hang.PAGESIZE);
            string resultPaging = paGing.CreatePaging(total, pCurrent, baseLink);
            if (string.IsNullOrEmpty(resultPaging))
                divPaging.Visible = false;
            else
            {
                divPaging.InnerHtml = resultPaging;
            }
        }
    }
}